/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;

using EnvDTE;
using EnvDTE80;

using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Library;

namespace DslEditorPowerToy.Recipes.Actions
{
    /// <summary>
    /// Adds project folders to referring project, corresponding to a relative folder path
    /// </summary>
    public class AddProjectFolderAction : ActionBase
    {
        private string _folderPath = string.Empty;
        private EnvDTE.Project _referringProject = null;
        private EnvDTE.ProjectItem _newFolder = null;

        #region Constructors
        public AddProjectFolderAction() : base() 
        { 
        }
        #endregion

        #region Public Properties
        [Input(Required=true)]
        public string FolderPath
        {
            get
            {
                return this._folderPath;
            }
            set
            {
                this._folderPath = value;
            }
        }
        [Input(Required=true)]
        [CLSCompliant(false)]
        public EnvDTE.Project ReferringProject
        {
            get
            {
                return this._referringProject;
            }
            set
            {
                this._referringProject = value;
            }
        }        
        [Output]
        [CLSCompliant(false)]
        public EnvDTE.ProjectItem NewFolder
        {
            get
            {
                return this._newFolder;
            }
        }        
        #endregion

        #region IAction Members

        public override void Execute()
        {
            base.Execute();
            ValidateParameters();

            //Split Path
            List<string> folderNames = GetFolderNames((string)this.Evaluate(this._folderPath));

            //Create folders (iteratively)
            ProjectItems currentProjectItems = this._referringProject.ProjectItems;
            foreach (string folderName in folderNames)
            {
                ProjectItem subFolderItem = DteHelper.FindItemByName(currentProjectItems, folderName, false);
                if ((null == subFolderItem) ||
                    (subFolderItem.Kind != Constants.vsProjectItemKindPhysicalFolder))
                {
                    //Create subfolder
                    subFolderItem = currentProjectItems.AddFolder(folderName, Constants.vsProjectItemKindPhysicalFolder);
                }

                //Iterate to sub folder
                currentProjectItems = subFolderItem.ProjectItems;

                //Save current folder
                this._newFolder = subFolderItem;
            }
        }
        public override void Undo()
        {
            //Do undo action, or explain why you can't or should not
        }

        #endregion

        #region Protected Methods
        /// <summary>
        /// Validates parameters before execution
        /// </summary>
        protected override void ValidateParameters()
        {
            if (null == this._folderPath)
                throw new ArgumentNullException("FolderPath");
            if (0 == this._folderPath.Length)
                throw new ArgumentOutOfRangeException("FolderPath");
            if (null == this._referringProject)
                throw new ArgumentNullException("ReferringProject");
        }
        #endregion

        #region Private Methods
        /// <summary>
        /// Returns a list of folder names in relative folder path
        /// </summary>
        /// <param name="folderPath"></param>
        /// <returns></returns>
        private List<string> GetFolderNames(string folderPath)
        {
            List<string> list = new List<string>();
            list.AddRange(folderPath.Split(new char[] { '\\' }));
            return list;
        }
        #endregion
    }
}
